package com.education.webservlet;

import com.education.db.HibernateSessionFactory;
import com.education.db.dao.InfoCatagoryDAO;
import com.education.db.dao.InfoSchoolDAO;
import com.education.db.domain.InfoCatagory;
import com.education.db.domain.InfoSchool;
import com.education.service.bean.ActivityBean;
import com.education.util.Constant;
import org.hibernate.Transaction;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fan307 on 2016/11/22.
 */
@WebServlet(name = "addMajorFolderSerlvet")
public class addMajorFolderSerlvet extends HttpServlet {

    private Transaction tran;

    public addMajorFolderSerlvet() {
        super();
    }

    public void destory(){
        super.destroy();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding(Constant.CHAR_ENCODING);

        tran = HibernateSessionFactory.getSession().getTransaction();

        try{
            tran.begin();
            InfoCatagoryDAO infoCatagoryDAO = new InfoCatagoryDAO();
            InfoSchoolDAO infoSchoolDAO = new InfoSchoolDAO();
            List<InfoCatagory> infoCatagories = infoCatagoryDAO.findAll();
            List<ActivityBean> infoCatagoryBean = new ArrayList<>();
            List<InfoSchool> infoSchools = infoSchoolDAO.findAll();
            List<ActivityBean> infoSchoolBean = new ArrayList<>();
            for(InfoCatagory infoCatagory:infoCatagories)
            {
                ActivityBean infocatagorybean = new ActivityBean();
                infocatagorybean.setId(infoCatagory.getCaId());
                infocatagorybean.setValue(infoCatagory.getCaValue());
                infocatagorybean.setName(infoCatagory.getCaName());
                infoCatagoryBean.add(infocatagorybean);
            }
            for(InfoSchool infoSchool:infoSchools)
            {
                ActivityBean infoschoolbean = new ActivityBean();
                infoschoolbean.setId(infoSchool.getScId());
                infoschoolbean.setValue(infoSchool.getScValue());
                infoschoolbean.setName(infoSchool.getScName());
                infoSchoolBean.add(infoschoolbean);
            }
            request.setAttribute("infocatagorybean",infoCatagoryBean);
            request.setAttribute("infoschoolbean",infoSchoolBean);
            request.getRequestDispatcher("/admin/addMajorFolder.jsp").forward(request, response);
            tran.commit();
        } catch (Exception e) {
            System.out.println(e.toString());
            tran.rollback();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void init() throws ServletException {

    }
}
